package com.yk;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo {
    public static void main(String[] args) {
        test0();
        test1();
        test2();
        test3();
    }

    public static void test0() {
        Map<String, Double> map = new HashMap<>();
        map.put("A", 1.0);
        map.put("B", 2.0);
        map.put("C", 3.0);
        map.put("D", 4.0);
        map.put("E", 5.0);
        map.put("F", 6.0);
        System.out.println(map);
        System.out.println("----------------------------------");
        map.remove("B");
        System.out.println(map);
        System.out.println("----------------------------------");
//        map.clear();
//        System.out.println(map);
//        System.out.println("----------------------------------");

        boolean containsKey = map.containsKey("A");
        System.out.println(containsKey);
        boolean containsValue = map.containsValue(3.0);
        System.out.println(containsValue);
        System.out.println("----------------------------------");

        boolean empty = map.isEmpty();
        System.out.println(empty);
        int size = map.size();
        System.out.println(size);
        System.out.println("----------------------------------");
    }

    public static void test1() {
        Map<String, Double> map = new HashMap<>();
        map.put("A", 1.0);
        map.put("B", 2.0);
        map.put("C", 3.0);
        map.put("D", 4.0);
        //将map中的键值取出来放在set集合中
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            //根据key取出value
            Double value = map.get(key);
            System.out.println(key + ":" + value);
        }
    }

    public static void test2() {
        Map<String, Double> map = new HashMap<>();
        map.put("A", 1.0);
        map.put("B", 2.0);
        map.put("C", 3.0);
        map.put("D", 4.0);
        //将map集合转成set集合
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //遍历set集合
        for (Map.Entry<String, Double> entry : entrySet) {
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key + ":" + value);

        }

    }
    public static void test3() {
        Map<String, Double> map = new HashMap<>();
        map.put("A", 1.0);
        map.put("B", 2.0);
        map.put("C", 3.0);
        map.put("D", 4.0);

//        map.forEach(new BiConsumer<String, Double>() {
//            @Override
//            public void accept(String key, Double value) {
//                System.out.println(key + ":" + value);
//            }
//        });

        map.forEach((key, value) -> System.out.println(key + ":" + value));
    }
}
